package activitys;

import io.web.JSONConnector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import misc.SpecialAdapter;
import nl.preflag.basewars.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * @author Patrick van Gils, 0787862, INF3M
 * Combatlog activity
 */

public class LogActivity extends Activity {
	
	private ImageView logMapButton;
	private int BASE_ID;
	public HashMap<String, String> rankMap;	
	private JSONConnector webby;
	public JSONArray logListJson, baseListJson;
	public HashMap<String, String> map, o;
	private String temp;

	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.log_list);
        
        Bundle extras = getIntent().getExtras();        
        BASE_ID = extras.getInt("base_id");
        
        setView();
        fillLog();
    }
	
	/**
	 * fill the views
	 */
	private void setView() {
		logMapButton = (ImageButton) findViewById(R.id.logMapButton);
			
		logMapButton.setOnClickListener(new View.OnClickListener() {
	          public void onClick(View v) {
	        	  openMapView();
	          }
		});
	}
	
	/**
	 * Fill the combat log
	 * Lelijk, maar boeit me niet meer!
	 */
	private void fillLog() {
		final ListView lv = (ListView) findViewById(R.id.logListView);
		String[] from = new String[] {"defense", "attack", "image"};
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    	try 
    	{
    		webby = new JSONConnector();
    		logListJson = webby.connect("/CombatLog/!%60attacking_base_id%60%20LIKE%20'"+BASE_ID+"'%20OR%20%60defending_base_id%60%20LIKE%20'"+BASE_ID+"'", "result", false);

    		for(int i=0;i<logListJson.length();i++) 
    		{
    			map = new HashMap<String, String>();	
    			JSONObject e = logListJson.getJSONObject(i);
    			if (e.getInt("attacking_base_id") == BASE_ID) {
    				map.put("attack",  "me");
    				map.put("image", ""+R.drawable.icon_won);
    			} else {
    				try 
    		    	{
	    				baseListJson = webby.connect("/Base/"+e.getInt("attacking_base_id")+"", "result", true);
	        			for (int j=0; j<baseListJson.length();j++) {
	        				JSONObject f = baseListJson.getJSONObject(j);
	        				temp = f.getString("name");
	        			}
    		    	} catch (Exception ei) {
    		    		temp = "No base name";
    		    	}
    				map.put("attack",  temp);
    				map.put("image", ""+R.drawable.icon_lost);
    			}
    			if (e.getInt("defending_base_id") == BASE_ID) {
    				map.put("defense",  "me");
    				map.put("image", ""+R.drawable.icon_lost);
    				
    			} else {
    				try 
    		    	{
	    				baseListJson = webby.connect("/Base/"+e.getInt("defending_base_id")+"", "result", true);
	        			for (int j=0; j<baseListJson.length();j++) {
	        				JSONObject f = baseListJson.getJSONObject(j);
	        				temp = f.getString("name");
	        			}
    		    	} catch (Exception ei) {
    		    		temp = "No base name";
    		    	}
    				map.put("defense",  temp);
    				map.put("image", ""+R.drawable.icon_won);
    			}
    			map.put("result", ""+e.getInt("won"));
    			fillMaps.add(map);
	
    		}
	     	
	   	} catch (Exception e) 
	   	{
	   		e.printStackTrace();
	   	}
    	// fill in the grid_item layout
        SpecialAdapter adapter = new SpecialAdapter(this, fillMaps, R.layout.log_list_item, from, new int[] { R.id.logDefenseBaseTextView, R.id.logAttackBaseTextView, R.id.logWinLossIcon});
        lv.setAdapter(adapter);
	}
	
	/**
	 * Open the map view
	 */
	private void openMapView() {
		finish();
	}
	
}
